package com.example.loganalysis.controller;


import com.example.loganalysis.dao.TerminalSessionsDao;

import com.example.loganalysis.entity.TerminalSession;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/logs")
public class TerminalSessionsController {

    private static final Logger logger = LoggerFactory.getLogger(TerminalSessionsController.class);

    private final TerminalSessionsDao terminalSessionsDao;

    @Autowired
    public TerminalSessionsController(TerminalSessionsDao terminalSessionsDao) {
        this.terminalSessionsDao = terminalSessionsDao;
    }

    @PostMapping("/insertTerminalSessions")
    public String insertLog(@RequestBody TerminalSession terminalSession) {
        logger.info("Received terminalsessions: {}", terminalSession);
        try {
            // 插入数据库
            terminalSessionsDao.insertTerminalSessions(terminalSession);
            logger.info("Terminal sessions inserted successfully: {}", terminalSession);
            return "Terminal sessions inserted successfully.";
        } catch (Exception e) {
            logger.error("Error inserting terminal sessions: {}", terminalSession, e);
            return "Error inserting terminal sessions.";
        }
    }

    @GetMapping("/terminal_ipfs_hash")
    public ResponseEntity<String> getTerminalIpfsHash(@RequestParam String log_id) {
        String ipfsHash = terminalSessionsDao.getIpfsHashByLogId(log_id);
        if (ipfsHash == null) {
            return ResponseEntity.status(HttpStatus.NOT_FOUND).body("Terminal IPFS Hash not found for log_id: " + log_id);
        }
        return ResponseEntity.ok(ipfsHash);
    }
}
